"""
Made by Wang Shiheng
CopyRight(C) From WangShiheng
All copyright(C) reserved
"""
# Author: WangShiheng
# Create Time:2022/05/03
# Package:

from util.OS.KeyBoard.KeyMap import KEYMAP
from util.base.LOGGER import LOGGER

import win32api
import win32con

__all__ = ['hotkey']


def hotkey(LOGGER=LOGGER, *args, **kwargs):
    keycodes = []
    for key in args:

        # 如果key为字符串，通过KEYMAP获得int值，加入keycodes中
        if isinstance(key, str):
            keycode = KEYMAP.setdefault(key.upper(), -1)
            if keycode == -1:
                if len(key) == 1:
                    try:
                        keycode = ord(key)
                    except:
                        LOGGER.warning(
                            f"The key {key} isn't in Key Map. But we find it in UnionString. is it {keycode}?")
                        continue
                    LOGGER.warning(f"The key {key} isn't in Key Map")

            keycodes.append(keycode)

        # 如果Key为int且在ASCII范围中，直接加入Keycodes中
        elif isinstance(key, int):
            if key <= 0:
                LOGGER.warning(f"The key {key} isn't in char points.")
                continue
            keycodes.append(key)

    _hotkey(keycodes)


def _hotkey(keycodes: list):
    temp = []
    while keycodes:
        key = keycodes.pop(0)
        win32api.keybd_event(key, 0, 0, 0)
        temp.append(key)

    while temp:
        key = temp.pop()
        win32api.keybd_event(key, 0, win32con.KEYEVENTF_KEYUP, 0)


if __name__ == '__main__':
    print(1)
    hotkey('control', 'v')
    hotkey('ctrlo')
    hotkey("或")
    print('stop')
